import 'package:elevator/model/build_detail.dart';
import 'package:elevator/model/group_info.dart';
import 'package:elevator/model/lift_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../model/build_info.dart';
import '../../utils/eventbus.dart';
import 'search_state.dart';

class SearchLogic extends GetxController {
  final SearchState state = SearchState();

  @override
  void onReady() {
    bus.on(bus.buildInfoKey, busCallback);
    super.onReady();
  }

  void initData(){
    state.allBuildList = Get.arguments["buildList"];
    state.psAuth = Get.arguments["psAuth"];
    state.user = Get.arguments["user"];
    if(null != state.allBuildList && state.allBuildList.isNotEmpty){
      for(BuildInfo buildInfo in state.allBuildList){
        if(buildInfo.buildDetail.buildSubZoneEn.isNotEmpty && state.buildZoneMap.containsKey(buildInfo.buildDetail.buildSubZoneEn)){
          List<BuildInfo>? buildInfList = state.buildZoneMap[buildInfo.buildDetail.buildSubZoneEn];
          buildInfList?.add(buildInfo);
        }else if(buildInfo.buildDetail.buildSubZoneEn.isNotEmpty && !state.buildZoneMap.containsKey(buildInfo.buildDetail.buildSubZoneEn)){
          List<BuildInfo> buildInfList = [buildInfo];
          state.buildZoneMap[buildInfo.buildDetail.buildSubZoneEn] = buildInfList;
          state.buildDetailList.add(buildInfo.buildDetail);
        }
      }
    }
    if(state.buildDetailList.isNotEmpty){
      onSelectBuildZoneChanged(state.buildDetailList[0]);
    }
  }

  busCallback(arg){
    update();
  }

  @override
  void onClose() {
    bus.off(bus.buildInfoKey, busCallback);
    super.onClose();
  }

  void onSelectBuildInfo(BuildInfo buildInfo) {
    onSelectBuildZoneChanged(buildInfo.buildDetail);
    onSelectBuildChanged(buildInfo);
  }

  void onSelectBuildZoneChanged(BuildDetail buildDetail){
    if(null!=buildDetail){
      state.selectBuildDetail.value = buildDetail;
      List<BuildInfo> buildInfoList = state.buildZoneMap[buildDetail.buildSubZoneEn]!;
      if(null!=buildInfoList && buildInfoList.isNotEmpty){
        state.buildList.value = buildInfoList;
        state.selectBuildInfo.value = state.buildList.value[0];
        onSelectBuildChanged(state.selectBuildInfo.value);
      }else{
        state.buildList.value = [];
        state.liftList.value = [];
        state.selectBuildInfo.value = BuildInfo();
      }
    }else{
      state.buildList.value = [];
      state.selectBuildInfo.value = BuildInfo();
      state.liftList.value = [];
    }
  }

  void onSelectBuildChanged(BuildInfo buildInfo){
    if(null!=buildInfo){
      state.liftList.value.clear();
      state.selectBuildInfo.value = buildInfo;
      for(LiftInfo liftInfo in buildInfo.liftInfoStateMaps.values){
        state.liftList.value.add(liftInfo);
      }
    }else{
      state.selectBuildInfo.value = BuildInfo();
      state.liftList.value = [];
    }
  }

  void goToSettingPage() {
    Get.toNamed("/setting",arguments: {"user":state.user});
  }
}
